<?php
namespace app\admin\controller;
use app\common\controller\Base;
class Common extends Base{
	function __construct(){
		parent::__construct();
    	$user=session(config('keys.admin_user'));
    	if(empty($user)){
    		$this->redirect(url('login/login'));
    	}
    	$this->user=$user;
		$this->assign('user',$user);
		$this->displayNavs($user['id']); //分配菜单
		$this->checkUserHasAuth($user['id']); //权限验证
		$this->assign('site_name',config('setting.admin_title'));

		$customers=model('customer')->lists();
		$this->assign('customers',json_encode($customers));
		$customer_counts=model('customer')->sum('is_read');
		$this->assign('customer_counts',$customer_counts);
    }
    /**
     * 删除 
     * @param  integer $id [description]
     * @return [type]      [description]
     */
	function del($id=0){
		if(request()->isAjax()||request()->isPost()){
			$ids=input('ids/a',[]); // 多项删除
	        if($id<=0&&empty($ids)){
	            $this->error(lang('Data does not exist'));
	        }
			$member_id=$this->user['id'];
			$where=['id'=>$id,'member_id'=>$member_id];
			if(!empty($ids)){
				$where=[
					'member_id'=>$member_id,
					'id'=>['id','in',$ids]
				];
			}
			
	        $del=model($this->table)->del($where);
	        if($del){
	            $this->success(lang('Successful operation'));
	            die;
	        }
	        $this->error(lang('Failed operation'));
	    }
    }
	// 文件上传
	function loads(){
		$file=request()->file('file');
		if($file){
			$rs=$this->uploads($file);
			if($rs){
				$rs['src']=images($rs['link']);
				return json(lang('Upload success'),0,$rs);
			}
			return json(lang('Upload failure'));
		}
	}
	// 上传头像
	function hloads($uid=0){
        $uids = $this->user['id'];
        // 获取超级管理员的用户id
        $suid = config('auth.super_id');
        
        // 判断当前用户是否是超级管理员
        if ($uid == $suid && $uids != $suid) {
            // 修改的是超级管理员的信息，单该用户不是管理员
            return json('权限不足',1);
        }
        if($uid<=0){
        	$uid=$uids;
        }
        $file=request()->file('file');
		if($file){
			$rs=$this->huploads($file,$validate=null,$tb=null,$uid);
			if($rs != ''){
				$user=model('member')->read(['id'=>$uid],'head_img');
		        $this->deleteUploadFile($user['head_img']);
				return json(lang('Upload success'),0,$rs);
			}
			return json(lang('Upload failure'),1);
		}
	}
	// 删除上传的文件
	function delImage($id=0){
		if($id>0){
			$has=model('media')->read(['id'=>$id]);
			if(empty($has)){
				return json(lang('Data does not exist'),1);
			}
			$del=model('media')->del($id);
			if(empty($has)){
				return json(lang('Failed operation'),1);
			}
			$file=env('root_path').'public/uploads/'.$has['link'];
	        if(is_file($file)){
	            unlink($file);
	        }
			return json(lang('Successful operation'));
		}
	}
	// 删除上传的文件
	function delImages($id=0,$where=[]){
		if($id>0||is_array($id)||count($where)){
			if(empty($where)){
				if(is_array($id)){
					$where=['id'=>['id','in',$id]];
				}else{
					$where=['id'=>$id];
				}
			}
			$has=model('media')->readAll($where,0,['field'=>'id,link']);
			if(empty($has)){
				$this->error(lang('Data does not exist'));
			}
			$del=model('media')->del($where);
			if(!$del){
				$this->error(lang('Failed operation'));
			}
			foreach ($has as $v) {
				$file=env('root_path').'public/uploads/'.$v['link'];
		        if(is_file($file)){
		            unlink($file);
		        }
			}
			$this->success(lang('Successful operation'));
		}
	}
	function getDevices(){
		if(request()->isAjax()||request()->isPost()){
			$ids=input('ids/a',[]);
			if(empty($ids)){
				$this->error('数据不存在');
			}
			$where=[
				'member_id'=>$this->user['id'],
				'id'=>['id','in',$ids]
			];
			$list=model('device')->readAll($where);
			if(count($list)){
				$this->success('获取成功','',$list->toJson());
			}
		}
	}
}
